package com.srit.base.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.springframework.orm.ibatis.SqlMapClientCallback;

import com.ibatis.sqlmap.client.SqlMapExecutor;
import com.srit.base.bean.Page;
import com.srit.base.dao.SqlMapDao;
import com.srit.base.ibatis.page.XSqlMapClientDaoSupport;

/**
 * sqlmap操作类实现
 * 
 * @author guan
 * 
 */
public class SqlMapDaoImpl extends XSqlMapClientDaoSupport implements SqlMapDao {
	private static final String PAGE_COUNT_HEAD = "count_";

	@Override
	public Object save(String statementName, Object obj) {
		return getSqlMapClientTemplate().insert(statementName, obj);
	}

	@Override
	public Object update(String statementName, Object obj) {
		return getSqlMapClientTemplate().update(statementName, obj);
	}

	@Override
	public int delete(String statementName, Object obj) {
		return getSqlMapClientTemplate().delete(statementName, obj);
	}

	@Override
	public List queryForList(String statementName, Object obj) {
		if (obj == null) {
			return getSqlMapClientTemplate().queryForList(statementName);
		}
		return getSqlMapClientTemplate().queryForList(statementName, obj);
	}

	@Override
	public void queryForPage(String statementName, Page page) {
		// 这里使用getSqlMapClientTemplate().queryForList方法，对查询方法进行重新修改
		// getSqlMapClientTemplate().queryForList(statementName, 0, 20);
		page.setList(getSqlMapClientTemplate().queryForList(statementName,
				page, page.getStart(), page.getPageSize()));
		if (statementName.indexOf(".") > -1) {
			statementName = statementName.replace(".", "." + PAGE_COUNT_HEAD);
		} else {
			statementName = PAGE_COUNT_HEAD + statementName;
		}
		page.setTotalNum((Integer) getSqlMapClientTemplate().queryForObject(
				statementName, page));
	}

	@Override
	public Object getById(String statementName, Integer id) {
		return getSqlMapClientTemplate().queryForObject(statementName, id);
	}

	@Override
	public void batchSave(final String statementName, final List objectList) {
		getSqlMapClientTemplate().execute(new SqlMapClientCallback() {
			@Override
			public Object doInSqlMapClient(SqlMapExecutor executor)
					throws SQLException {
				executor.startBatch();
				for (int i = 0; i < objectList.size(); i++) {
					executor.insert(statementName, objectList.get(i));
				}
				executor.executeBatch();
				return null;
			}
		});
	}

	@Override
	public void batchUpdate(final String statementName, final List objectList) {
		getSqlMapClientTemplate().execute(new SqlMapClientCallback() {
			@Override
			public Object doInSqlMapClient(SqlMapExecutor executor)
					throws SQLException {
				executor.startBatch();
				for (int i = 0; i < objectList.size(); i++) {
					executor.update(statementName, objectList.get(i));
				}
				executor.executeBatch();
				return null;
			}
		});
	}

	@Override
	public Object queryForObject(String statementName, Object object) {
		return getSqlMapClientTemplate().queryForObject(statementName, object);
	}

}
